package com.ruoyi.common.core.web.domain;

import cn.hutool.http.HttpStatus;

import java.util.HashMap;

/**
 * 操作消息提醒
 *
 * @author ruoyi
 */
public class AjaxResult<T> extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    private static final String CODE_TAG = "code";

    /** 返回内容 */
    private static final String MSG_TAG = "msg";

    /** 数据对象 */
    private static final String DATA_TAG = "data";

    private static final String TIMESTAMP_TAG = "timestamp";

    private static final String SUCCESS_TAG = "success";

    protected AjaxResult(){
        this.setSuccess(true);
        this.setTimestamp(System.currentTimeMillis());
        this.setCode(cn.hutool.http.HttpStatus.HTTP_OK);
        this.setMsg("操作成功");
    }

    public static AjaxResult success(){
        AjaxResult result = new AjaxResult();
        return result;
    }

    public static AjaxResult success(Object data){
        AjaxResult result = new AjaxResult();
        result.setData(data);
        return result;
    }

    public T getData(){
        return (T) this.get(DATA_TAG);
    }
    /**
     * 已过期
     * @param msg
     * @return
     */
    @Deprecated
    public static AjaxResult success(String msg){
        AjaxResult result = new AjaxResult();
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult success(String msg, Object data){
        AjaxResult result = new AjaxResult();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult error(){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(cn.hutool.http.HttpStatus.HTTP_INTERNAL_ERROR);
        result.setMsg("操作失败");
        return result;
    }

    public static AjaxResult error(String msg){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(cn.hutool.http.HttpStatus.HTTP_INTERNAL_ERROR);
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult error(int code, String msg){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult error(String msg, Object data){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public AjaxResult setSuccess(boolean success){
        super.put(SUCCESS_TAG, success);
        return this;
    }

    public AjaxResult setData(T data){
        super.put(DATA_TAG, data);
        return this;
    }

    public AjaxResult setCode(int code){
        super.put(CODE_TAG, code);
        return this;
    }

    public AjaxResult setMsg(String msg){
        super.put(MSG_TAG, msg);
        return this;
    }

    public AjaxResult setTimestamp(long timestamp){
        super.put(TIMESTAMP_TAG, timestamp);
        return this;
    }

    public AjaxResult put(String key, Object val){
        super.put(key, val);
        return this;
    }
}
